﻿Public Class Lvl_Data_Compare

    '用
    Public Structure ComparePoint
        Public Name As String
        Public Level As Double
    End Structure

    Public cmpPnts As List(Of ComparePoint)

    Sub Add(ByVal Name As String, ByVal Level As Single)
        Dim vP As New ComparePoint
        vP.Name = Name
        vP.Level = Level
        cmpPnts.Add(vP)
    End Sub

    Sub Add(ByVal vP As ComparePoint)
        cmpPnts.Add(vP)
    End Sub

    Function CompareHeight(ByVal svName As String, ByVal cmpName As String) As Single
        Dim svLvl As Single, svFound As Boolean = False
        Dim cmpLvl As Single, cmpFound As Boolean = False
        For Each cp1 As ComparePoint In cmpPnts
            If Not svFound Then
                If cp1.Name = svName Then
                    svLvl = cp1.Level
                    svFound = True
                End If
            End If
            If Not cmpFound Then
                If cp1.Name = cmpName Then
                    cmpLvl = cp1.Level
                    cmpFound = True
                End If
            End If

            If svFound And cmpFound Then
                Return svLvl - cmpLvl
            End If
        Next

        Return 999999
    End Function

    Function RoundRead(ByVal svName As String, ByVal cmpName As String, ByVal cmpRead As Long) As Long
        Dim ht As Single = CompareHeight(svName, cmpName)
        If ht < 999999 Then
            Return cmpRead - ht * 100000
        Else
            Return 999999
        End If
    End Function

    Function RoundRead(ByRef Stn As Lvl_Data_Station, ByVal SvPntName As String) As Long
        Dim cmpName As String = String.Empty, cmpRead As Long
        Dim Found As Boolean = False

        If Stn.BackPoint.ValidPoint Then
            cmpName = Stn.BackPoint.Name
            cmpRead = Stn.BackPoint.AverageRead
            Found = True
        ElseIf Stn.ForePoint.ValidPoint Then
            cmpName = Stn.ForePoint.Name
            cmpRead = Stn.ForePoint.AverageRead
            Found = True
        Else
            For I As Integer = 0 To Stn.MidPointCount - 1
                Dim vP As Lvl_Data_Point = Stn.MidPoint(I)
                If vP.ValidPoint Then
                    cmpName = vP.Name
                    cmpRead = vP.AverageRead
                    Found = True
                    Exit For
                End If
            Next
        End If
        If Found Then
            Return RoundRead(SvPntName, cmpName, cmpRead)
        Else
            Return 999999
        End If
    End Function


End Class
